using UnityEngine;
using System.Collections.Generic;
using System.Text;

public static class TransformUtil
{

    public static void RemoveAllChildren(Transform a)
    {
        if (a == null)
        {
            return;
        }
        for (int i = 0; i < a.childCount; i++)
        {
            var go = a.GetChild(i).gameObject;
            if (Application.isPlaying)
            {
                GameObject.Destroy(go);
            }
            else
            {
                GameObject.DestroyImmediate(go);
            }
        }
        a.DetachChildren();
    }

    public static void HideAllChildren(Transform t)
    {
        if (t == null)
        {
            return;
        }
        for (int i = 0; i < t.childCount; i++)
        {
            Transform child = t.GetChild(i);
            child.gameObject.SetActive(false);
        }
    }

    public static List<Transform> GetChildList(Transform t)
    {
        var ret = new List<Transform>();
        var childCount = t.childCount;
        for(int i = 0; i < childCount; i++)
        {
            var child = t.GetChild(i);
            ret.Add(child);
        }
        return ret;
    }

    public static string GetPath(Transform t)
    {
        if(t == null) 
        {
            return "";
        }
        var sb = new StringBuilder();
        sb.Append(t.name);
        while(t.parent != null)
        {
            t = t.parent;
            sb.Insert(0, t.name + "/");
        }
        return sb.ToString();
    }

    public static void SetChildSolo(Transform root, string childName)
    {
        foreach(Transform child in root)
        {
            if(child.name == childName)
            {
                child.gameObject.SetActive(true);
            }
            else
            {
                child.gameObject.SetActive(false);
            }
        }
    }


    public static bool IsTransformRectOverlap(RectTransform rt1, RectTransform rt2)
    {
        Bounds bounds1 = RectTransformToWorld(rt1);
        Bounds bounds2 = RectTransformToWorld(rt2);
        bool isOverlap = bounds1.Intersects(bounds2);
        return isOverlap;
    }

    public static Bounds RectTransformToWorld(RectTransform rt)
    {
        Vector3[] corners = new Vector3[4];
        rt.GetLocalCorners(corners);
        for (int i = 0; i < 4; i++)
        {
            corners[i] = rt.TransformPoint(corners[i]);
        }
        Bounds bounds = new Bounds(corners[0], Vector3.zero);
        for (int i = 1; i < 4; i++)
        {
            bounds.Encapsulate(corners[i]);
        }
        return bounds;
    }

    public static bool IsRectVerticalContains(RectTransform rt1, float rt1YExt, RectTransform rt2, float rt2YExt)
    {
        var rect1 = rt1.rect;
        var rect2 = rt2.rect;
        rect1.yMin -= rt1YExt;
        rect1.yMax += rt1YExt;
        rect2.yMin -= rt2YExt;
        rect2.yMax += rt2YExt;
        rect1 = TransformRect(rt1, rect1);
        rect2 = TransformRect(rt2, rect2);
        if (rect1.yMin <= rect2.yMin && rect1.yMax >= rect2.yMax)
        {
            return true;
        }
        return false;
    }

    public static bool IsRectVerticalInsect(RectTransform rt1, float rt1YExt, RectTransform rt2, float rt2YExt)
    {
        var rect1 = rt1.rect;
        var rect2 = rt2.rect;
        rect1.yMin -= rt1YExt;
        rect1.yMax += rt1YExt;
        rect2.yMin -= rt2YExt;
        rect2.yMax += rt2YExt;
        rect1 = TransformRect(rt1, rect1);
        rect2 = TransformRect(rt2, rect2);
        if (rect1.yMin > rect2.yMax || rect1.yMax < rect2.yMin)
        {
            return false;
        }
        return true;
    }

    public static Rect TransformRect(RectTransform rectTransform, Rect rect)
    {
        var point = rect.position;
        var size = rect.size;
        var pointWorld = rectTransform.TransformPoint(point);
        var sizeWorld = rectTransform.TransformVector(size);
        var worldRect = new Rect(pointWorld, sizeWorld);
        return worldRect;
    }

    public static Rect InverseTransformRect(RectTransform rectTransform, Rect rect)
    {
        var point = rect.position;
        var size = rect.size;
        var pointWorld = rectTransform.InverseTransformPoint(point);
        var sizeWorld = rectTransform.InverseTransformVector(size);
        var worldRect = new Rect(pointWorld, sizeWorld);
        return worldRect;
    }

    public static void SetPivotKeepRectStay(RectTransform rectTransform, Vector2 pivot)
    {
        var localRect = rectTransform.rect;
        var worldRect = TransformUtil.TransformRect(rectTransform, localRect);
        var parentRect = TransformUtil.InverseTransformRect(rectTransform.parent.GetComponent<RectTransform>(), worldRect);
        rectTransform.pivot = pivot;
        rectTransform.localPosition = new Vector2(parentRect.x + parentRect.width * rectTransform.pivot.x, parentRect.y + parentRect.height * rectTransform.pivot.y);
    }
}